<?php

class bot{

  function BotUpdate() {
      global $resource, $reslist, $CONF, $db, $PLANET, $USER;
      
      require_once(ROOT_PATH.'includes/classes/class.PlanetRessUpdate.php'); 
    
    $PlanetRess = new ResourceUpdate();

    $allbotsrow = $db->query('SELECT id, last_update, level FROM '.BOTS.'');
    
    while ($AllBots = $db->fetch_array($allbotsrow))
                {
                
                  $Time = time() - (3600 * (1 + ($AllBots['level'] * 1)));
                  $Updatetime = $AllBots['last_update'] + (3600 * (1 + ($AllBots['level'] * 1)));
              
                  if ($AllBots['last_update'] <= $Time)
                  {
                  
                      $BuildingsArrayUser = CurrentBuildArray($AllBots['level']);
                      
                              $Qry    = "LOCK TABLE ".BOTS." as b WRITE, ".USERS." as u WRITE;
                                         UPDATE ".BOTS." as b, ".USERS." as u  SET";
                                   
                           if (!empty($BuildingsArrayUser['research']))
                           {
                             foreach($BuildingsArrayUser['research'] as $Element => $Level)
                             {
                              $Qry    .= "`u`.`".$resource[$Element]."` = '".$Level."', ";
                             }
                           }

                              $Qry    .= "`b`.`last_update` = '".$Updatetime."',
                                          `b`.`level` = `b`.`level`+1
                                          WHERE
                                          `b`.`id` = '".$AllBots['id'] ."' AND
                                          `u`.`id` = '".$AllBots['id']."';
                                          UNLOCK TABLES;";

                  }
                  
                  $allbotsplanetsrow = $db->query('SELECT * FROM '.PLANETS.' WHERE id_owner = '.$AllBots['id'].'');
                   
                      while ($AllBotsPlanets = $db->fetch_array($allbotsplanetsrow ))
                      {
                      
                      $BotUser   = $db->uniquequery("SELECT * FROM ".USERS." WHERE `id` = '".$BotPlanet['id_owner']."';");
                      
                      $TimePlanet = time() - (3600 * (1 + ($AllBotsPlanets['level'] * 1)));
                      $UpdatetimePlanet = $AllBotsPlanets['last_update_bot'] + (3600 * (1 + ($AllBotsPlanets['level'] * 1)));
                      
                      list($BotUser['factor'], $AllBotsPlanets['factor'])    = getFactors($BotUser, $AllBotsPlanets);
                      list($BotUser, $AllBotsPlanets) = $PlanetRess->CalcResource($BotUser, $AllBotsPlanets, true, TIMESTAMP);
                      $PlanetRess->SavePlanetToDB();

                      BotSaveFleet($BotUser, $AllBotsPlanets);
                       
                      if ($AllBotsPlanets['last_update_bot'] <= $TimePlanet)
                        {
                      
                          $BuildingsArray = CurrentBuildArray($AllBotsPlanets['level']);
                    
                              $Qry    = "LOCK TABLE ".PLANETS." as p WRITE;
                                         UPDATE ".PLANETS." as p SET";
                                   
                           if (!empty($BuildingsArray['building']))
                           {
                             foreach($BuildingsArray['building'] as $Element => $Level)
                             {
                              $Qry    .= "`p`.`".$resource[$Element]."` = '".$Level."', ";
                             }
                           }
                           if (!empty($BuildingsArray['zivil']))
                           {
                             foreach($BuildingsArray['zivil'] as $Element => $Level)
                             {
                              $Qry    .= "`p`.`".$resource[$Element]."` = '".$Level."', ";
                             }
                           }

                              $Qry    .= "`p`.`last_update_bot` = '".$UpdatetimePlanet."',
                                          `p`.`level` = `p`.`level`+1
                                          WHERE
                                          `p`.`id` = '".$AllBotsPlanets['id']."';
                                          UNLOCK TABLES;";
                              $db->multi_query($Qry);
                     
                        }
                    }
      }
  
  }
  
   function BotSaveFleet($BotUser, $BotPlanet) {
      global $db, $LNG;
      
      $Addfleet = $db->query("SELECT `fleet_owner`, `fleet_start_time` FROM ".FLEETS." WHERE fleet_target_owner = '".$BotUser['id']."' AND fleet_end_id = '".$BotPlanet['id']."' ");
      
     if($Addfleet) { 
     
                  list($BotUser['factor'], $BotPlanet['factor'])    = getFactors($BotUser, $BotPanet);
                  list($BotUser, $BotPlanet) = $PlanetRess->CalcResource($BotUser, $BotPlanet, true, $Addfleet['fleet_start_time']);
     
     $SaveChance = mt_rand(0,1);
     
       if ($SaveChance == 1) {
         
         $FleetEndTime = TIMESTAMP + 7200;
         $FleetStartTime = TIMESTAMP + 3600;
         $TransportMetal = $BotPlanet['metal'];
         $TransportCrystal = $BotPlanet['crystal'];
         $TransportDeuterium = $BotPlanet['deuterium'];
                                                         
         
     $QryInsertFleet  = "LOCK TABLE ".FLEETS." WRITE, ".PLANETS." WRITE;
                                                        INSERT INTO ".FLEETS." SET
                                                        `fleet_owner` = '".$BotUser['id']."',
                                                        `fleet_mission` = '12',
                                                        `fleet_amount` = '".$FleetShipCount."',
                                                        `fleet_array` = '".$fleet_array."',
                                                        `fleet_universe` = '1',
                                                        `fleet_start_time` = '".$FleetStartTime ."',
                                                        `fleet_start_id` = '".$BotPlanet['id']."',
                                                        `fleet_start_galaxy` = '".$BotPlanet['galaxy']."',
                                                        `fleet_start_system` = '".$BotPlanet['system']."',
                                                        `fleet_start_planet` = '".$BotPlanet['planet']."',
                                                        `fleet_start_type` = '1',
                                                        `fleet_end_time` = '".$FleetEndTime."',
                                                        `fleet_end_stay` = '".$FleetEndTime."',
                                                        `fleet_end_id` = '".$BotPlanet['id']."',
                                                        `fleet_end_galaxy` = '".$BotPlanet['galaxy']."',
                                                        `fleet_end_system` = '".$BotPlanet['system']."',
                                                        `fleet_end_planet` = '".$BotPlanet['planet']."',
                                                        `fleet_end_type` = '1',
                                                        `fleet_resource_metal` = '".$TransportMetal."',
                                                        `fleet_resource_crystal` = '".$TransportCrystal."',
                                                        `fleet_resource_deuterium` = '".$TransportDeuterium."',
                                                        `fleet_target_owner` = '".$BotUser['id']."',
                                                        `fleet_group` = '0',
                                                        `start_time` = '".TIMESTAMP."';
                                                        UPDATE `".PLANETS."` SET
                                                        ".substr($FleetSubQRY,0,-2)."
                                                        WHERE
                                                        `id` = ". $BotPlanet['id'] ." LIMIT 1;
                                                        UNLOCK TABLES;";


                $db->multi_query($QryInsertFleet);
                
                $BotMessageSend = mt_rand(0,1);
                
         if($BotMessageSend == 1){
         
         $BotMessageSendID = mt_rand(1,5);
         
           switch($BotMessageSendID){
             case '1':
                $BotMessage = $LNG['BotMessage1'];
                $BotMessageSubject = $LNG['BotMessageSubject1'];
             break;
             case '2':
                $BotMessage = $LNG['BotMessage2'];
                $BotMessageSubject = $LNG['BotMessageSubject2'];
             break;
             case '3':
                $BotMessage = $LNG['BotMessage3'];
                $BotMessageSubject = $LNG['BotMessageSubject3'];
             break;
             case '4':
                $BotMessage = $LNG['BotMessage4'];
                $BotMessageSubject = $LNG['BotMessageSubject4'];
             break;
             case '5':
                $BotMessage = $LNG['BotMessage5'];
                $BotMessageSubject = $LNG['BotMessageSubject5'];
             break;
                                   }                             
            
            $From    = $BotUser['username'].' ['.$BotUser['galaxy'].':'.$BotUser['system'].':'.$BotUser['planet'].']';          
                
            SendSimpleMessage($Addfleet['fleet_owner'], $BotUser['id'], '', 1, $From, $BotMessageSubject, $BotMessage);

         }
                
                $BotPlanet['metal']                -= $TransportMetal;
                $BotPlanet['crystal']              -= $TransportCrystal;
                $BotPlanet['deuterium']            -= $TransportDeuterium;

                $PlanetRess->SavePlanetToDB();
       }
     
     }
  
  }
  
  private function CurrentBuildArray($Level) {
  
    switch($Level)
                {
                case 0:
                         $Buildarray['building'] = array(                       
                                              1 => 1,
                                              2 => 1,
                                              3 => 1,
                                              4 => 3
                                            );
                 break;
                 case 1:
                         $Buildarray['building'] = array(                       
                                              1 => 3,
                                              2 => 3,
                                              3 => 3,
                                              4 => 6
                                            );
                 break;
                 case 2:
                         $Buildarray['building'] = array(                       
                                              1 => 6,
                                              2 => 6,
                                              3 => 6,
                                              4 => 8
                                            );
                 break;
                 case 3:
                         $Buildarray['building'] = array(                       
                                               14 => 3,
                                               21 => 3,
                                               31 => 3,
                                               22 => 2,
                                               23 => 2,
                                               24 => 2
                                             );
                 break;
                 case 4:
                         $Buildarray['building'] = array(                       
                                                1 => 9,
                                                2 => 9,
                                                3 => 8,
                                                4 => 11
                                              );
                         $Buildarray['zivil'] = array(                       
                                                801 => 3,
                                                802 => 5,
                                                808 => 3
                                               );
                 break;
                 case 5:
                         $Buildarray['building'] = array(                       
                                                 1 => 15,
                                                 2 => 15,
                                                 3 => 13,
                                                 4 => 16
                                               );
                         $Buildarray['zivil'] = array(                       
                                                801 => 5,
                                                802 => 10,
                                                808 => 5,
                                                815 => 3,
                                                );
                        $Buildarray['research'] = array(                       
                                                 113 => 3,
                                                 106 => 1,
                                                 108 => 1
                                                 );
                  break;
                  case 6:
                         $Buildarray['building'] = array(                       
                                                  1 => 17,
                                                  2 => 16,
                                                  3 => 15,
                                                  4 => 18,
                                                  12 => 5,
                                                  14 => 5
                                                );
                         $Buildarray['zivil'] = array(                       
                                                 801 => 7,
                                                 802 => 12,
                                                 826 => 2,
                                                 827 => 3
                                                 );
                         $Buildarray['research'] = array(                       
                                                  106 => 2,
                                                  108 => 2
                                                  );
                   break;
                   case 7:
                          $Buildarray['building'] = array(                       
                                                   1 => 18,
                                                   2 => 16,
                                                   3 => 16,
                                                   4 => 18,
                                                  12 => 7,
                                                  14 => 8,
                                                  31 => 6,
                                                  21 => 5
                                                 );
                          $Buildarray['zivil'] = array(
                                                  802 => 14,
                                                  826 => 4,
                                                  827 => 5
                                                  );
                   break;
                   case 8:
                          $Buildarray['building'] = array(                       
                                                  12 => 9,
                                                  14 => 10,
                                                  31 => 8,
                                                  21 => 7,
                                                  22 => 5,
                                                  23 => 5,
                                                  24 => 5,
                                                  44 => 2
                                                 );
                          $Buildarray['zivil'] = array(
                                                  802 => 16,
                                                  826 => 5,
                                                  827 => 6,
                                                  815 => 5,
                                                  828 => 1
                                                  );
                         $Buildarray['research'] = array(                       
                                                  115 => 2,
                                                  106 => 5,
                                                  113 => 4,
                                                  108 => 3
                                                  );                        
                   break;
                   case 9:
                           $Buildarray['building'] = array(
                                                   21 => 8,
                                                   31 => 9
                                                  );
                           $Buildarray['zivil'] = array(
                                                   826 => 6,
                                                   827 => 7,
                                                   828 => 3
                                                   );
                           $Buildarray['research'] = array(                       
                                                   108 => 5,
                                                   117 => 2
                                                   );                      
                    break;
                    case 10:
                            $Buildarray['building'] = array(
                                                    1 => 20,
                                                    2 => 19,
                                                    3 => 19,
                                                    4 => 20,
                                                    12 => 13
                                                   );
                            $Buildarray['zivil'] = array(
                                                    825 => 3,
                                                    826 => 8,
                                                    827 => 9,
                                                    828 => 5
                                                    );
                            $Buildarray['research'] = array(                       
                                                    108 => 7,
                                                    115 => 4
                                                    );
                    break;
                    case 11:
                             $Buildarray['building'] = array(
                                                    21 => 10,
                                                    31 => 12
                                                    );
                             $Buildarray['zivil'] = array(
                                                    825 => 5,
                                                    826 => 9,
                                                    827 => 10,
                                                    828 => 7
                                                     );
                             $Buildarray['research'] = array(                       
                                                     108 => 9,
                                                     115 => 6
                                                     );                   
                     break;
                     case 12:
                              $Buildarray['building'] = array(
                                                     21 => 10,
                                                     31 => 12
                                                     );
                              $Buildarray['zivil'] = array(
                                                     802 => 18
                                                      );
                              $Buildarray['research'] = array(                       
                                                     108 => 10,
                                                     111 => 2
                                                     );                  
                      break;
                      case 13:
                               $Buildarray['building'] = array(
                                                      21 => 12
                                                      );
                               $Buildarray['zivil'] = array(
                                                      802 => 20
                                                       );
                               $Buildarray['research'] = array(                       
                                                      117 => 4,
                                                      120 => 5,
                                                      121 => 2,
                                                      111 => 2
                                                      );                     
                       break;
                        case 14:
                                $Buildarray['building'] = array(
                                                       21 => 14
                                                       );
                                $Buildarray['zivil'] = array(
                                                       802 => 22,
                                                       825 => 7,
                                                       826 => 11,
                                                       827 => 13,
                                                       828 => 9
                                                        );
                                $Buildarray['research'] = array(                       
                                                       113 => 6,
                                                       110 => 5,
                                                       121 => 2,
                                                       114 => 3,
                                                       118 => 4,
                                                       111 => 2
                                                       );                      
                        break;
                        case 15:
                                 $Buildarray['building'] = array(
                                                        21 => 16
                                                        );
                                 $Buildarray['zivil'] = array(
                                                        802 => 24,
                                                        825 => 9,
                                                        826 => 13,
                                                        827 => 14,
                                                        828 => 12
                                                         );
                                 $Buildarray['research'] = array(
                                                        131 => 4,
                                                        132 => 4,
                                                        133 => 4,
                                                        134 => 3
                                                        );
                         break;
                         case 16:
                                  $Buildarray['building'] = array(
                                                         21 => 18,
                                                         15 => 1
                                                         );
                                  $Buildarray['zivil'] = array(
                                                         802 => 24,
                                                         825 => 9,
                                                         826 => 13,
                                                         827 => 14,
                                                         828 => 12
                                                          );
                                  $Buildarray['research'] = array(
                                                         131 => 4,
                                                         132 => 4,
                                                         133 => 4,
                                                         134 => 3
                                                         );
                                                         
                          break;
                          case 17:
                                   $Buildarray['building'] = array(
                                                          1 => 22,
                                                          2 => 20,
                                                          3 => 20,
                                                          4 => 21,
                                                          12 => 15
                                                          );
                                   $Buildarray['zivil'] = array(
                                                          802 => 24,
                                                          825 => 9,
                                                          826 => 13,
                                                          827 => 14,
                                                          828 => 12
                                                           );                      
                           break;
                }
    
    
    return $Buildarray;
  
  }


}

?>